<?php

namespace App\Http\Controllers\api\Ali;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;

class AliPayController extends Controller
{

    protected $config = [
        'app_id' => '2021000122606105',
        'notify_url' => 'http://81.68.244.9/api/notify',
        'return_url' => 'http://81.68.244.9/api/return',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzQwRGJuMxF4SMkJ+X9M5n5+8mvwjfkVHU0XAWtvRnjzEuBzqE/a1OjxrQI/m6FxyhCfPIhDjSq0vJb/oLQS1ElVb1ccRY8bJTMmayHsWFdxYR3xTc8dHWboI+bduq56PQY7b9nOIhgNWcPdqDsnpjjsn4oVdMGHiUt6d9bS0NEouPEVgF0lY/gncvyaXBZ9C16R0dqJKwwHhGFyzNx7fHlO5uA5LlKZP1peSipei7Z9mVMT+E/nV7YdZTSoNUP5048lKYVQv1jSciEDd+6s/H0MScBPEjdZSp9j7n2b+rh4zUFM7Pbax9Xc0rMUEMOaJluZDLFYOLXcBTFfkhhQOyQIDAQAB',
        // 加密方式： **RSA2**
        'private_key' => 'MIIEpQIBAAKCAQEAzQwRGJuMxF4SMkJ+X9M5n5+8mvwjfkVHU0XAWtvRnjzEuBzqE/a1OjxrQI/m6FxyhCfPIhDjSq0vJb/oLQS1ElVb1ccRY8bJTMmayHsWFdxYR3xTc8dHWboI+bduq56PQY7b9nOIhgNWcPdqDsnpjjsn4oVdMGHiUt6d9bS0NEouPEVgF0lY/gncvyaXBZ9C16R0dqJKwwHhGFyzNx7fHlO5uA5LlKZP1peSipei7Z9mVMT+E/nV7YdZTSoNUP5048lKYVQv1jSciEDd+6s/H0MScBPEjdZSp9j7n2b+rh4zUFM7Pbax9Xc0rMUEMOaJluZDLFYOLXcBTFfkhhQOyQIDAQABAoIBAQCVTXwFVv4g3f6R6ijqzXrf6AgFMiMGBpvw3pE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mVVNX+ahWeBccSSRR1qoLfxL2EUl2p97fDiMf5NXptfZr7BynKWrEKvhEhcXOlWtaPRv2CKIh5//5KpB7riCvcNqVdER+bJc9CuWdf3FlKzYow8Q=',
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
    ];
    protected $configApi = [
        'app_id' => '2021000122606105',
        'notify_url' => 'http://81.68.244.9/api/notify',
        'return_url' => 'http://81.68.244.9/api/return',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsYlsbkFD9UeujiMRQMqvnsqr/dchpdzdcIXhAPNAjbVZogEpXnkopC84MIZ2JJqTpsVk0XlHd6wB0521aebk5/UeuDpNwHd0WYBj7zezZSoXc7LTgjZQhW07AB+atnkB/e2qeyHDA6mB6w2yDjAcPib7d3vB67clsZqFrkmG6KCymwmxAg3d5dnbF+O5r2Ul02wKL06ucNgTqsj69mBq+QHzFZ5rIaMxliSK3yDSsYyx8/cfeh7mRQ4Epd4XWBxyhZfhZJBdb85ikXd3n41LWS0qsfvMUDcTjdgqX64i6E6Em3HBd/8dulAWOs4L9tkQbh5YVVq7RqQRL65FgY0S1QIDAQAB',
        // 加密方式： **RSA2**
        'private_key' => 'MIIEpQIBAAKCAQEAzQwRGJuMxF4SMkJ+X9M5n5+8mvwjfkVHU0XAWtvRnjzEuBzqE/a1OjxrQI/m6FxyhCfPIhDjSq0vJb/oLQS1ElVb1ccRY8bJTMmayHsWFdxYR3xTc8dHWboI+bduq56PQY7b9nOIhgNWcPdqDsnpjjsn4oVdMGHiUt6d9bS0NEouPEVgF0lY/gncvyaXBZ9C16R0dqJKwwHhGFyzNx7fHlO5uA5LlKZP1peSipei7Z9mVMT+E/nV7YdZTSoNUP5048lKYVQv1jSciEDd+6s/H0MScBPEjdZSp9j7n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ihcoj68VHWYPwj6kHjnm25fF6leRa7gr/Kp8wOikCgYEArexWYGyRyXn+mOcERNyv2o4wsthXeYnb0MVlLZDB1cTVMJWgBpZ2mTbWiSfmVVNX+ahWeBccSSRR1qoLfxL2EUl2p97fDiMf5NXptfZr7BynKWrEKvhEhcXOlWtaPRv2CKIh5//5KpB7riCvcNqVdER+bJc9CuWdf3FlKzYow8Q=',
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
    ];
    /**
     * @zhifu
     * @user wsg
     * @method post
     * @url https://www.healthcare.com/api/index
     * @data
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $order_no = $request->all();
        $order = [
            'out_trade_no' => $order_no['order_no'],
            'total_amount' => 60,
            'subject' => 'Wsg',
        ];

//        print_r($order);die();
        $alipay = Pay::alipay($this->config)->web($order);

        return $alipay;// laravel 框架中请直接 `return $alipay`
    }
    /**
     * @同步回调
     * @user wsg
     * @method post
     * @url https://www.healthcare.com/api/index
     * @data
     * @return \Illuminate\Http\Response
     */
    public function return(Request $request)
    {
//        $data = Pay::alipay($this->config)->verify(); // 是的，验签就这么简单！

         //订单号：
        $data = $request->all();
//        dd($data);
        return view('payment/paysuccess',['data' => $data]);
        // 支付宝交易号：$data->trade_no
        // 订单总金额：$data->total_amount
//       return view('/payment/paysuccess',['data' => $data]);
    }
    /**
     * @异步回调
     * @user wsg
     * @method post
     * @url https://www.healthcare.com/api/index
     * @data
     * @return \Illuminate\Http\Response
     */
    public function notify(Request $request)
    {
        $params = $request->all();
        Cache::put('a',$params);
        DB::beginTransaction();
        try {
            $currentTime = date('Y-m-d H:i:s',time());

            if ($params['trade_status'] == 'TRADE_SUCCESS'){
                Cache::put('b','b');
                DB::table('order_form')
                    ->where('order_no',$params['out_trade_no'])
                    ->update([
                        'trade_no' => $params['trade_no'],
                        'status' => 4,
                        'pay_method' => 1,
                        'updated_at' => $currentTime
                    ]);
                //获取订单表里的库存商品id
                Cache::put('F','F');
                DB::commit();
                echo "success";
            }
        }catch (\Exception $e){
            DB::rollBack();
            \Illuminate\Support\Facades\Log::info($e->getMessage());
        }
    }
    public function cache()
    {
        $aa = Cache::get('a');
        print_r($aa);
        $bb = Cache::get('b');
        print_r($bb);
        $cc = Cache::get('c');
        print_r($cc);
        $cc = Cache::get('d');
        print_r($cc);
        $cc = Cache::get('E');
        print_r($cc);
        $cc = Cache::get('F');
        print_r($cc);
    }

    /*
     * 退款
     */
    public function redund($out_trade_no)
    {

        $order = [
            'out_trade_no' => $out_trade_no,
            'refund_amount' => 60,
        ];
        $alipay = Pay::alipay($this->configApi);
        $result = $alipay->refund($order);
        DB::table('order_form')
            ->where('order_no',$result['out_trade_no'])
            ->update([
                'status' => 7,
            ]);
        return $result;
    }

}
